﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace Bowlus.Common
{
    public class AppHostInstance
    {
        public string Name { get; set; }

        public string Status { get; set; }

        public string Host { get; set; }

        // TODO: Change this to use the service management API
        public static IList<AppHostInstance> GetAppHostInstances()
        {
            var webfarmRole = RoleEnvironment.Roles[Settings.AppHostRoleName];

            var hostInstances = (from instance in webfarmRole.Instances
                                 let endpoint = instance.InstanceEndpoints.First(e => e.Key == Settings.AppHostEndpointName)
                                 select new AppHostInstance()
                                            {
                                                Host = new Uri("http://" + endpoint.Value.IPEndpoint.ToString()).Host,
                                                Name = instance.Id
                                            }).ToList();

            return hostInstances;
        }
    }
}
